package com.quad.innovators.salesease.controller.warehouse;

import com.alibaba.excel.EasyExcel;
import com.quad.innovators.salesease.annotation.DataPermission;
import com.quad.innovators.salesease.annotation.FunctionPermission;
import com.quad.innovators.salesease.annotation.Log;
import com.quad.innovators.salesease.annotation.RepeatSubmit;
import com.quad.innovators.salesease.common.result.PageResultBean;
import com.quad.innovators.salesease.common.result.Result;
import com.quad.innovators.salesease.constants.SystemConstants;
import com.quad.innovators.salesease.enums.ArrivalScanStatusEnum;
import com.quad.innovators.salesease.enums.LogModuleEnum;
import com.quad.innovators.salesease.excel.IdentityCodeListener;
import com.quad.innovators.salesease.model.excel.IdentityCodeExcel;
import com.quad.innovators.salesease.model.form.warehouse.ArrivalOrderForm;

import com.quad.innovators.salesease.model.query.warehouse.ArrivalOrderPageQuery;

import com.quad.innovators.salesease.model.vo.warehouse.ArrivalOrderVO;
import com.quad.innovators.salesease.service.warehouse.ArrivalOrderService;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

/**
 * @author : shanshan.li
 * @createTime : 2024/12/17 15:42
 * @Version : 1.0 
 */
@Tag(name = "12.到货单管理")
@RestController
@RequestMapping(SystemConstants.BACKEND_API + "/arrival-order")
@RequiredArgsConstructor
public class ArrivalOrderController {

    private final ArrivalOrderService arrivalOrderService;
    private final IdentityCodeListener identityCodeListener;

    @Operation(summary = "分页列表")
    @GetMapping("/page")
    @Log(value = "分页列表", module = LogModuleEnum.ARRIVAL_ORDER)
    @DataPermission(permissionCode = "warehouse:arrival-order:data-permission")
    public Result<PageResultBean<ArrivalOrderVO>> listPaged(ArrivalOrderPageQuery queryParams) {
        PageResultBean<ArrivalOrderVO> result = arrivalOrderService.listPage(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "新增")
    @PostMapping
    @FunctionPermission(permissionCode = "warehouse:arrival-order:add")
    @RepeatSubmit
    public Result<?> save(@RequestBody @Valid ArrivalOrderForm form) {
        boolean result = arrivalOrderService.save(form);
        return Result.success(result);
    }

    @Operation(summary = "表单数据")
    @GetMapping("/{code}/form")
    @Parameters({@Parameter(name = "code", description = "code"),})
    @FunctionPermission(permissionCode = "warehouse:arrival-order:detail")
    @DataPermission(permissionCode = "warehouse:arrival-order:data-permission")
    public Result<ArrivalOrderForm> getForm(@PathVariable("code") String code) {
        ArrivalOrderForm formData = arrivalOrderService.getFormData(code);
        return Result.success(formData);
    }

    @Operation(summary = "修改")
    @PutMapping(value = "/{code}")
    @Parameters({@Parameter(name = "code", description = "code")})
    @FunctionPermission(permissionCode = "warehouse:arrival-order:edit")
    public Result<?> update(@PathVariable("code") String code, @RequestBody @Validated ArrivalOrderForm form) {
        boolean result = arrivalOrderService.update(code, form);
        return Result.success(result);
    }

    @Operation(summary = "去小程序扫码")
    @PutMapping(value = "/{code}/arrivalScan")
    @Parameters({@Parameter(name = "code", description = "code")})
    @FunctionPermission(permissionCode = "warehouse:arrival-order:edit")
    public Result<?> arrivalScan(@PathVariable("code") String code) {
        boolean result = arrivalOrderService.arrivalScan(code, ArrivalScanStatusEnum.SCANNING.getCode());
        return Result.success(result);
    }

    @Operation(summary = "导入到货码")
    @PostMapping(value = "/{code}/importScan")
    @Parameters({@Parameter(name = "code", description = "code")})
    // @FunctionPermission(permissionCode = "warehouse:arrival-order:add")
    public Result<?> excelImport(@RequestPart MultipartFile file, @PathVariable String code) throws IOException {
        identityCodeListener.setArrivalCode(code);
        InputStream inputStream = file.getInputStream();
        EasyExcel.read(inputStream).head(IdentityCodeExcel.class)
                .sheet(0)
                .registerReadListener(identityCodeListener) // 注册自定义监听器
                .headRowNumber(1)       //行头所在行位置
                .doRead();
        return Result.success("成功");
    }

    @Operation(summary = "获取小程序详情信息")
    @GetMapping(value = "/{code}/getH5Detail")
    @Parameters({@Parameter(name = "code", description = "code")})
    // @FunctionPermission(permissionCode = "warehouse:arrival-order:add")
    public Result<ArrivalOrderForm> getH5Detail(@PathVariable String code) throws IOException {
        ArrivalOrderForm formData = arrivalOrderService.getFormDataH5(code);
        return Result.success(formData);
    }
}
